package com.chang.cheng.toutiaoserver.utils;

import java.sql.Timestamp;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class LocalDateUtil {

    public static Date localDateToDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static LocalDate dateToLocalDate(Date date) {
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
    }

    public static LocalDateTime dateToLocalDateTime(Date date) {
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static java.sql.Date localDateTimeToSqlDate(LocalDateTime lt) {
        long l = lt.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        return new java.sql.Date(l);
    }

    public static String LDTToString(LocalDateTime lt){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String dateTimeStr = df.format(lt);
        return dateTimeStr;
    }
    public static LocalDateTime stringToLDT(String dateTimeStr){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr, df);
        return dateTime;
    }
    public static Long stringTOTimeStamp(String dateTimeStr){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr, df);
        Timestamp timestamp = Timestamp.valueOf(dateTime);
        return timestamp.getTime();
    }
    public static String stampToString(Long dateTimeL){
        LocalDateTime time2 =LocalDateTime.ofEpochSecond(dateTimeL/1000,0,ZoneOffset.ofHours(8));
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String dateTimeS = time2.format(df);
        return dateTimeS;
    }
    public static Long LDTToMillisecond(){
        LocalDateTime data=LocalDateTime.now();
        DateTimeFormatter dtf7 = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        String strDate7 = dtf7.format(data);
        Long longDate = Long.parseLong(strDate7);
        return longDate;
    }

    public static Long LDTToSecond(){
        LocalDateTime data=LocalDateTime.now();
        DateTimeFormatter dtf7 = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String strDate7 = dtf7.format(data);
        Long longDate = Long.parseLong(strDate7);
        return longDate;
    }
    /**
     * The ISO date formatter that formats or parses a date with the
     * offset if available, such as '2011-12-03' or '2011-12-03+01:00'.
     * @param lt
     * @return
     */
    public static String localDateToString(LocalDate lt){
        DateTimeFormatter format = DateTimeFormatter.ISO_DATE;
        String date = format.format(lt);
        return  date;
    }

    public static LocalDateTime stringToLocalDateTime(String str){
        //自定义格式
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.parse(str, df);
        return localDateTime;
    }
}
